package com.stu.dcxy.juc;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicMarkableReference;

/**
 * @Author: MI
 * @Date: 2022/3/10/22:27
 * @Description:
 */
//https://cdn.jsdelivr.net/gh/qianlicao51/files@main/img/2022-03/20220310222617.png
public class AtomicMarkableReferenceDemo {
    static AtomicMarkableReference atomicMarkableReference = new AtomicMarkableReference<>(100, false);

    public static void main(String[] args) {

        new Thread(() -> {
            boolean marked = atomicMarkableReference.isMarked();
            System.out.println(Thread.currentThread().getName() + "\t" + marked);
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            atomicMarkableReference.compareAndSet(100, 110, marked, !marked);
        }).start();
        new Thread(() -> {
            boolean marked = atomicMarkableReference.isMarked();
            System.out.println(Thread.currentThread().getName() + "\t" + marked);
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            boolean andSet = atomicMarkableReference.compareAndSet(100, 110, marked, !marked);
            System.out.println("操作是否成功" + andSet);
        }).start();

    }
}
